//
//  MJTEmoticonKeyBoard.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/19.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
//  重用标记
private let MJTEmoticonCollectionViewCellIdentifier = "MJTEmoticonCollectionViewCellIdentifier"
class MJTEmoticonKeyBoard: UIView {

    //MARK:  懒加载控件
    private lazy var toolBar: MJTEmoticonToolBar = {
        let toolbar = MJTEmoticonToolBar(frame: CGRectZero)
        return toolbar
    }()
    //表情键盘视图
    private lazy var emoticonCollectionView: UICollectionView = {
        //布局视图
        let flowLayout = UICollectionViewFlowLayout()
        //设置水平滚动
        flowLayout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        //去掉水平垂直滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        //开启分页
        view.pagingEnabled = true
        //去掉弹簧效果
        view.bounces = false
        view.registerClass(MJTEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: MJTEmoticonCollectionViewCellIdentifier)
        // 数据源代理
        view.dataSource = self
        // 设置代理
        view.delegate = self
        view.backgroundColor = self.backgroundColor

        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(35)
        }
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        //闭包回调
        toolBar.selectedToolBarButtonClosure = { [weak self] (type: MJTEmoticonToolBarButtonType) in
            let indexPath: NSIndexPath
            switch type {
            case .Nomal:
//                print("Normal")
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
//                print("Emoji")
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .lxh:
//                print("lxh")
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
            }
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }

    }
    //重写layoutSubView
    override func layoutSubviews() {
        super.layoutSubviews()
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = emoticonCollectionView.size
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
    }

}
extension MJTEmoticonKeyBoard: UICollectionViewDataSource,UICollectionViewDelegate {
    //返回多少组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return MJTEmoticonTools.shareTools.allWmoticonArray.count
    }
    //返回每组多少个
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return MJTEmoticonTools.shareTools.allWmoticonArray[section].count
    }
    //为cell赋值
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MJTEmoticonCollectionViewCellIdentifier", forIndexPath: indexPath) as! MJTEmoticonCollectionViewCell
        cell.indexPath = indexPath
        cell.emoticons = MJTEmoticonTools.shareTools.allWmoticonArray[indexPath.section][indexPath.item]
        return cell
    }
    // 监听滚动获取cell
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let contentOffSetX = scrollView.contentOffset.x
        //获取当前显示cell,无序的,需要我们进行排序然后在判断cell哪个显示的多
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            //根据Frame的x进行排序
            return firstCell.x < secondCell.x
        }
        //判断哪个显示多
        if cells.count == 2 {
            //获取第一个cell
            let firstCell = cells.first!
            //获取第二个cell
            let secondCell = cells.last!
            let firstCellContentOffSetX = abs(firstCell.x - contentOffSetX)
            let secondCellContenOffSetX = abs(secondCell.x - contentOffSetX)

            //如果第一个差值小于第二个差值,则第一个cell显示的多
            let indexPath: NSIndexPath
            if firstCellContentOffSetX < secondCellContenOffSetX {
//                print("第一个cell显示的多")
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
            } else {
//                print("第二个cell显示多")
                indexPath = emoticonCollectionView.indexPathForCell(secondCell)!
            }
            //获取indexPath对应的section
            let section = indexPath.section
            //设置按钮选中
            toolBar.selectEmotionButtonWithSection(section)

        }
    }
}
